﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WLFramework.Data;
using WLFramework.Data.Linq;
using WLFramework.Data.DataProvider;
using System.Diagnostics;
using System.Data;
using System.Configuration;
using WLFramework.DataAccess;
using WLFramework.Reflection;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        static UnitTest1()
        {
            DbManager.AddDataProvider(new PostgreSQLDataProvider());
        }

        public long Takes(Action action)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            action();

            return watch.ElapsedMilliseconds;
        }

        public string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
            }
        }

        [TestMethod]
        public void TestMethod1()
        {
            long takes = Takes(() =>
            {
                using (LocalDBContext context = new LocalDBContext())
                {
                    var list = context.test.OrderBy(k => k.id).ToList();
                    //Assert.AreEqual(list.Count, 10, 
                    //    string.Format("takes {0} ms", watch.ElapsedMilliseconds));
                }
            });

            Assert.Inconclusive(string.Format("takes {0} ms", takes));
        }

        [TestMethod]
        public void TestMethod2()
        {
            long takes = Takes(() =>
            {
                using (var conn = new Npgsql.NpgsqlConnection(ConnectionString))
                using (var cmd = conn.CreateCommand())
                {
                    conn.Open();
                    cmd.CommandText = "select * from test";

                    DataTable dt = new DataTable();
                    dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
                }
            });

            Assert.Inconclusive(string.Format("takes {0} ms", takes));
        }

        [TestMethod]
        public void TestMethod3()
        {
            // 测试: 向数据库中插入一条数据
            long takes = Takes(() =>
            {
                using (var context = new LocalDBContext())
                {
                    try
                    {
                        context.BeginTransaction();
                        context.role.Insert(() => new role()
                        {
                            rolename = "action1"
                        });

                        Assert.IsNotNull(context.role.Where(k => k.rolename == "action1").FirstOrDefault());
                    }
                    finally
                    {
                        context.RollbackTransaction();
                    }
                }
            });

            Assert.Inconclusive(string.Format("takes {0} ms", takes));
        }

        public class Test
        {
            public int Id { get; set; }
            public string Name { get; set; }

            public string Remark;

            public int Add(int x, int y)
            {
                return x + y;
            }
        }

        [TestMethod]
        public void TestMethod4()
        {
            var type = TypeAccessor.GetAccessor(typeof(Test));
            object test = type.CreateInstance();
            foreach (MemberAccessor member in type)
            {
                Debug.WriteLine(member.Name);
            }

            Assert.IsNotNull(test);
        }

        [TestMethod]
        public void TestMethod5()
        {
            string json = fastJSON.JSON.Instance.ToJSON(new DateTime(2000, 1, 1));
            Debug.WriteLine(json);
            Assert.Inconclusive(json);
            Assert.IsNotNull(json);
        }
    }
}
